package num102;

import com.sun.source.tree.Tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author : DuJiabao
 * @Project : sword-finger-offer
 * @Time : 2021/5/9 17:30
 * @Desc :
 */
public class Solution2 {
  public List<List<Integer>> levelOrder(TreeNode root) {
    Queue<TreeNode> queue = new LinkedList<>();
    ArrayList<List<Integer>> res = new ArrayList<>();
    if (root == null) return res;
    queue.offer(root);
    while (!queue.isEmpty()) {
      int size = queue.size();
      List<Integer> depth = new ArrayList<>();
      for (int i = 0; i < size; i++) {
        TreeNode node = queue.poll();
        depth.add(node.val);
        if (node.left != null) queue.offer(node.left);
        if (node.right != null) queue.offer(node.right);
      }
      res.add(depth);
    }
    return res;
  }

  public static void main(String[] args) {
    TreeNode node = new TreeNode(3);
    node.left = new TreeNode(9);
    node.right = new TreeNode(20);
    node.right.left = new TreeNode(15);
    node.right.right = new TreeNode(7);
    List<List<Integer>> res = new Solution2().levelOrder(node);
    System.out.println(res);
  }
}
